#include "LightHandlerThread.h"
#include <iostream>
#include "Lightenums.h"

using namespace std;

LightHandlerThread::LightHandlerThread(LightHandler* lightHandler,
		const int action, const int sleep):
	OpenThreads::Thread(), _quitflagL(false),
		_lightHandler(lightHandler), _actionL(action), _sleepL(sleep) {}

LightHandlerThread::~LightHandlerThread() {
	#ifdef DEBUG
		cout << "----- Destruiert: LightHandlerThread -----" << endl;
	#endif
}


/*
 * run-Methode wird beim starten des Threads aufgerufen.
 * Entscheidet mittels des Integers action welche Aktion
 * ausgeführt werden soll. Jede Aktion ruft eine
 * Methode aus dem LightHandler auf.
 */

void LightHandlerThread::run() {	    
while (!_quitflagL) {      
	//_lightHandler->getLockMutex()->lock();
	switch(_actionL)
		{
		case SWITCHLIGHTONE:
			_lightHandler->createLights();
			_quitflagL = true;
			break;
		case SWITCHLIGHTTWO:
	 		_lightHandler->removeLights();
			_quitflagL = true;
			break;
		case SWITCHLIGHTTHREE:
	 		_lightHandler->discoOrbSpot(true, osg::Vec4d(1.0, 0.0, 0.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			_lightHandler->discoOrbSpot(true, osg::Vec4d(1.0, 1.0, 0.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			_lightHandler->discoOrbSpot(true, osg::Vec4d(0.0, 0.0, 1.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			_lightHandler->discoOrbSpot(true, osg::Vec4d(1.0, 0.0, 1.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			_lightHandler->discoOrbSpot(true, osg::Vec4d(0.0, 1.0, 1.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			break;
		case SWITCHLIGHTFOUR:
	 		_lightHandler->softSwitch(FOUR);
			break;
		case SWITCHLIGHTFIVE:
			_lightHandler->softSwitch(FIVE);
			break;
		case SWITCHLIGHTSIX:
	 		_lightHandler->strobo(true);
			OpenThreads::Thread::microSleep(_sleepL*5);
			_lightHandler->strobo(false);
			OpenThreads::Thread::microSleep(_sleepL*5);
			break;
		case SWITCHLIGHTSEVEN:
			_lightHandler->toggleCouchSpot(osg::Vec4d(1.0, 0.0, 0.0, 1.0));
			_quitflagL = true;
			break;
		case SWITCHLIGHTEIGHT:
			_lightHandler->createDefaultLightning();
			_quitflagL = true;
			break;
		case SWITCHLIGHTNINE:
			_lightHandler->softSwitchRandom(FOUR);
			break;
		case SWITCHLIGHTTEN:
			_lightHandler->softSwitchRandom(FIVE);
			break;
		case SWITCHLIGHTELEVEN:
			_lightHandler->discoOrbSpot(true, osg::Vec4d(0.0, 0.0, 0.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			_lightHandler->discoOrbSpot(true, osg::Vec4d(1.0, 0.0, 0.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			_lightHandler->discoOrbSpot(true, osg::Vec4d(1.0, 0.8, 0.0, 1.0));
			OpenThreads::Thread::microSleep(_sleepL*50);
			break;
		default:
			#ifdef DEBUG
			cout << "Unknown command!" << endl;
			#endif
			break;
		}
	//_lightHandler->getLockMutex()->unlock();
}	
}	

/*
 * Resettet das quitFlag
 */

void LightHandlerThread::reset() {
	_quitmutexL.lock();
	_quitflagL = false;
	_quitmutexL.unlock();
}

/*
 * Beendet den Thread über setzen eines quitFlags
 */

void LightHandlerThread::quit() {	
	_quitmutexL.lock();
	_quitflagL = true;
	_quitmutexL.unlock();	
}
